<?php
/**
 * Created by PhpStorm.
 * User: *!N.J
 * Date: 2021/8/22
 * Time: 4:29
 * QQ:1467572213
 */

namespace app\en\model;

use think\Model;
use think\Db;
use think\facade\Config;

class Order extends Model
{
    public static function index($user_id, $type)
    {
        if ($type == null) $type = 0;
        switch ($type) {
            case 0:
                $where['uid'] = $user_id;
                $where['status'] = $type;
                $order_list = Db::name('fd_order')->where($where)->order('id', 'asc')->select();
                    // ->paginate(1, false, [
                    //     'query' => request()->param(),
                    //     'type' => 'page\page',
                    //     'var_page' => 'page',
                    // ]);
                return $order_list;
                break;
            case 1:
            case 2:
            case 3:
                $where['uid'] = $user_id;
                $where['status'] = $type;
                $order_list = Db::name('fd_order')->where($where)->order('id', 'asc')->select();
                    // ->paginate(5, false, [
                    //     'query' => request()->param(),
                    //     'type' => 'page\page',
                    //     'var_page' => 'page',
                    // ]);
                return $order_list;
                break;
        }

    }

    public static function order_finish($id, $user_info)
    {
        $where['id'] = $id;
        $where['uid'] = $user_info['id'];
        $order_info = Db::name('fd_order')->where($where)->find();
        if ($user_info['balance'] >= $order_info['goods_price']) {
            if ($order_info['status'] == 0) {
                $dec_money = self::dec_money($user_info, $order_info['goods_price']);
                if ($dec_money == 'success') {
                    $update['status'] = 2;
                    $update['user_balance'] = $user_info['balance'] - $order_info['goods_price'];
                    $update['settle_time'] = time() + 30;
                    $update['update_time'] = time();
                    $order_finish = Db::name('fd_order')->where($where)->update($update);
                    if ($order_finish == true) {
                        $map['uid'] = $user_info['id'];
                        $map['status'] = 0;
                        $check_order = Db::name('fd_order')->where($map)->order('id', 'asc')->find();
                        if ($check_order) {
                            return ['status' => 'success_go', 'data' => $check_order];
                        } else {
                            return ['status' => 'success'];
                        }
                    } else {
                        return ['status' => 'error'];
                    }
                } else {
                    return ['status' => 'error_dec'];
                }
            } else {
                return ['status' => 'error_status'];
            }
        } else {
            return ['status' => 'error_money'];
        }
    }

    public static function dec_money($user_info, $money)
    {
        $dec_money = Db::name('fd_user')->where('id', $user_info['id'])->setDec('balance', $money);
        $dec_freeze = Db::name('fd_user')->where('id', $user_info['id'])->setInc('freeze', $money);
        if ($dec_money == true && $dec_freeze == true) {
            return 'success';
        } else {
            return 'error';
        }
    }

    public static function info($user_id, $id)
    {
        $where['id'] = $id;
        $where['uid'] = $user_id;
        $info = Db::name('fd_order')->where($where)->find();
        return $info;
    }

    public static function check_timeout($order_id)
    {
        $now_time = time();
        $where = [
            ['id', '=', $order_id],
            ['status', '=', 0],
            ['sub_time', '<', $now_time],
        ];
        $check = Db::name('fd_order')->where($where)->find();
        if ($check) {
            Db::name('fd_order')->where($where)->setField('status', 3);
            return ['status'=>1,'data'=>$check];
        } else {
            return ['status'=>0];
        }
    }

    public static function check_settlement($order_id)
    {
        $where = [
            ['id', '=', $order_id],
            ['status', '=', 1],
        ];
        $check = Db::name('fd_order')->where($where)->find();
        if ($check) {
            return ['status'=>1,'data'=>$check];
        } else {
            return ['status'=>0];
        }
    }

}